package br.com.academia.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.academia.beans.Plano;

public class PlanoDAO {
	private Connection connection;
	
	public PlanoDAO(Connection connection) {
		this.connection = connection;
	}
	
	public List<String[]> consultaPlanos(List<String> dadosConsulta)  {
		List<String[]> retorno = new ArrayList<String[]>();
		String query = "Select * from Planos";
		 try {
			 PreparedStatement stm = connection.prepareStatement(query);
			 ResultSet rs = stm.executeQuery();
			 while(rs.next()) {
				 String[] row = new String[dadosConsulta.size()];
				 for(int i = 0; i < row.length; i++) {
					 row[i] = rs.getString(dadosConsulta.get(i));
				 }
				 retorno.add(row);
			 }
		 } catch(SQLException e) {
			 e.printStackTrace();
		 }
		return retorno;
	}
	
	public List<Plano> buscaPlanosSelecao() {
		List<Plano> listaPlano = new ArrayList<Plano>();
		String query = "Select idPlano, nome from Planos";

		try {
			PreparedStatement stm = connection.prepareStatement(query);
			ResultSet rs = stm.executeQuery();

			while (rs.next()) {
				Plano plano = new Plano();
				plano.setIdPlano(rs.getInt("idPlano"));
				plano.setNome(rs.getString("nome"));
				listaPlano.add(plano);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return listaPlano;
	}
	
	public Plano consultaPlanoId(int idPlano) {
		Plano plano = new Plano();
		String query = "Select * from Planos where idPlano = ?";
		try {
			PreparedStatement stm = connection.prepareStatement(query);
			stm.setInt(1, idPlano);
			ResultSet rs = stm.executeQuery();
			
			if(rs.next()) {
				plano.setIdPlano(rs.getInt("idPlano"));
				plano.setNome(rs.getString("nome"));
				plano.setValor(rs.getDouble("valor"));
				plano.setDescricao(rs.getString("descricao"));
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		
		return plano;
	}

}
